package im.route.api.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(DateUtil.class);
    public static String FULL_DATETIME_FORMAT = "yyyyMMddHHmmss";

    public static String convertToString(Date date, String format) {
        if (date == null) {
            return null;
        }

        if (format == null) {
            format = FULL_DATETIME_FORMAT;
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        return dateFormat.format(date);
    }

    public static String convertToString(LocalDateTime localDateTime, String format) {
        if (localDateTime == null) {
            return null;
        }

        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        return convertToString(date, format);
    }

    public static XMLGregorianCalendar convertToXMLGregorianCalendar(Date date) {
        try {
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(date);
            XMLGregorianCalendar result = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
            result.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
            return result;
        } catch (DatatypeConfigurationException e) {
            LOGGER.error(e.getMessage());
        }

        return null;
    }

    public static XMLGregorianCalendar convertStringToXMLGregorianCalendar(String dateStr, String format) {
        try {
            SimpleDateFormat formatter = new SimpleDateFormat(format);
            Date date = formatter.parse(dateStr);
            GregorianCalendar gc = new GregorianCalendar();
            gc.setTime(date);
            XMLGregorianCalendar result = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
            result.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
            return result;
        } catch (DatatypeConfigurationException e) {
            LOGGER.error(e.getMessage());
        } catch (ParseException e) {
            LOGGER.error(e.getMessage());
        }

        return null;
    }

    public static Date convertStringToDate(String dateStr, String format) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            Date date = dateFormat.parse(dateStr);
            return date;
        } catch (ParseException e) {
            LOGGER.error(e.getMessage());
        }

        return null;
    }

    public static Date convertStringToDate(String dateStr, String format, boolean isThrowError) throws ParseException {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            Date date = dateFormat.parse(dateStr);
            return date;
        } catch (ParseException e) {
            LOGGER.error(e.getMessage());

            if (isThrowError) {
                throw e;
            }
        }

        return null;
    }

    public static Date add(Date date, int type, int value) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(type, value);
        return calendar.getTime();
    }
}
